package com.kgc.controller;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname BaseController
 * @Description 控制层基类
 * @Date 2021/6/23 16:03
 * @Created by 17402
 */
public class BaseController {
  /**
   * @description: 获取参数，获取不到，置空
   * @param:
   * @return:
   * @author 17402
   * @date: 2021/6/23
   */
    protected String getParameter(HttpServletRequest request,String key){
      return request.getParameter(key)==null?"":request.getParameter(key);

    }
    /**
     * @description: 获取参数，获取不到，设置位默认值
     * @param:
     * @return:
     * @author 17402
     * @date: 2021/6/23
     */
    protected String getParameterAsDefault(HttpServletRequest request, String key,String defaultValue) {
        return request.getParameter(key) == null ? defaultValue : request.getParameter(key);

     }
     /**
      * @description: 获取客户端请求IP
      * @param:
      * @return:
      * @author 17402
      * @date: 2021/6/23
      */
    protected static String getRemoteIp(HttpServletRequest request) {
        // 获取ip
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
